//
//  MKReaderBottomView.swift
//  WXReader
//
//  Created by huaya on 2025/8/30.
//  Copyright © 2025 Andrew. All rights reserved.
//

import UIKit
import Foundation

enum MKReaderBVButtonType:NSInteger {
    case save
    case comment
    case dayNight
    case previous
    case next
}

typealias MKReaderBVActionBlock = (MKReaderBVButtonType) -> Void

class MKReaderBottomView: UIView {
    
    @IBOutlet weak var saveImgView: UIImageView!
    @IBOutlet weak var commentImgView: UIImageView!
    @IBOutlet weak var nightDayImgView: UIImageView!
    @IBOutlet weak var prevImgView: UIImageView!
    @IBOutlet weak var nextImgView: UIImageView!
    
    @IBOutlet weak var saveLab: UILabel!
    @IBOutlet weak var commentLab: UILabel!
    @IBOutlet weak var nightDayLab: UILabel!
    @IBOutlet weak var prevLab: UILabel!
    @IBOutlet weak var nextLab: UILabel!
    
    @IBOutlet weak var bgView1: UIView!
    @IBOutlet weak var bgView2: UIView!
    @IBOutlet weak var bgView3: UIView!
    @IBOutlet weak var bgView4: UIView!
    @IBOutlet weak var bgView5: UIView!
    
    @IBOutlet weak var btnSave: UIButton!
    @IBOutlet weak var btnComment: UIButton!
    @IBOutlet weak var btnNight: UIButton!
    @IBOutlet weak var btnPrev: UIButton!
    @IBOutlet weak var btnNext: UIButton!
    
    @objc var isShow:Bool = true
    @objc var isCollect:Bool = false
    
    @objc var btnActionBlock: ((NSInteger) -> Void)?
    
    @objc class func createView() -> MKReaderBottomView?{
        let view:MKReaderBottomView =  Bundle.main.loadNibNamed("MKReaderBottomView", owner: nil, options: nil)?.last as! MKReaderBottomView
        view.frame = CGRect(x: 0,
                            y: SCREEN_HEIGHT - MKReaderBottomViewHeight,
                            width: SCREEN_WIDTH,
                            height: MKReaderBottomViewHeight)
        return view
    }
    
    @objc private func buttonAction(button:UIButton){
        let btnType = MKReaderBVButtonType(rawValue: button.tag)
        if (btnType == .dayNight){
            btnNight.isSelected = !btnNight.isSelected
            if (btnNight.isSelected){
                MKReaderCfg.shared().bgColorIndex = 4
            }else{
                MKReaderCfg.shared().bgColorIndex = 0
            }
            MKReaderCfg.shared().saveConfigure()
            
        }
        refreshStyleUI()
        btnActionBlock?(btnType!.rawValue)
    }
    
    @objc func showView(show:Bool){
        if show == isShow {
            return
        }
        UIView.animate(withDuration: MKREADER_ANIMATE_DURATION,
                       delay: 0,
                       options: .curveEaseOut) {[weak self] () in
            let y = show ? SCREEN_HEIGHT - MKReaderBottomViewHeight : SCREEN_HEIGHT
            self?.frame.origin = CGPoint(x: 0, y: y)
        } completion: { [weak self] (ok) in
            self?.isShow = show
        }
    }
    
    @objc func refreshStyleUI(){
        
        if MKReaderCfg.shared().bgColorIndex == 0{
            
            backgroundColor = .white
            
            saveImgView.image = .init(named: "hy_wsc")
            commentImgView.image = .init(named: "ydy_pl")
            nightDayImgView.image = .init(named: "ydy_yj")
            prevImgView.image = .init(named: "ydy_syp")
            nextImgView.image = .init(named: "ydy_xyp")
            
            saveLab.textColor = .hex_333333()
            commentLab.textColor = .hex_333333()
            nightDayLab.textColor = .hex_333333()
            prevLab.textColor = .hex_333333()
            nextLab.textColor = .hex_333333()
            
        }else{
            
            backgroundColor = .hex_191820()
            
            saveImgView.image = .init(named: "hy_wsc_yj")
            commentImgView.image = .init(named: "ydy_pl_yj")
            nightDayImgView.image = .init(named: "ydy_ri")
            prevImgView.image = .init(named: "ydy_syp_yj")
            nextImgView.image = .init(named: "ydy_xyp_yj")
            
            saveLab.textColor = .white
            commentLab.textColor = .white
            nightDayLab.textColor = .white
            prevLab.textColor = .white
            nextLab.textColor = .white
            
        }
        if self.isCollect {
            saveImgView.image = UIImage(named: "hy_ysc")
        }
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        btnSave.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        btnSave.tag = MKReaderBVButtonType.save.rawValue
        
        btnComment.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        btnComment.tag = MKReaderBVButtonType.comment.rawValue
        
        btnNight.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        btnNight.tag = MKReaderBVButtonType.dayNight.rawValue
        
        btnPrev.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        btnPrev.tag = MKReaderBVButtonType.previous.rawValue
        
        btnNext.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        btnNext.tag = MKReaderBVButtonType.next.rawValue
        
        refreshStyleUI()
    }

    @objc func canPrevStatus(canUse : Bool){
            
        if canUse {
            
            prevImgView.alpha = 1
            prevLab.alpha = 1
            btnPrev.isUserInteractionEnabled = true
        }else{
            
            prevImgView.alpha = 0.5
            prevLab.alpha = 0.5
            btnPrev.isUserInteractionEnabled = false
        }
        
        
    }
    
    @objc func canNextStatus(canUse : Bool){
            
        if canUse {
            nextImgView.alpha = 1
            nextLab.alpha = 1
            btnNext.isUserInteractionEnabled = true
        }else{
            nextImgView.alpha = 0.5
            nextLab.alpha = 0.5
            btnNext.isUserInteractionEnabled = false
        }
        
    }
    @objc func collectStatus(collect : Bool){
        
        self.isCollect = collect
        
        if collect {
            saveImgView.image = UIImage(named: "hy_ysc")
        }else{
            if MKReaderCfg.shared().bgColorIndex == 0{
                saveImgView.image = UIImage(named: "hy_wsc")
            }else{
                saveImgView.image = UIImage(named: "hy_wsc_yj")
            }
        }
        
    }
}
